package org.whuims.core.dataprocessor;

import org.whuims.core.utils.Mysql;
import org.whuims.entity.Term;

import java.io.File;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Qikai on 2016/11/26.
 */
public class TermUpdater {
    Map<String, Long> map = new HashMap<String, Long>();
    private String dirPath = "src/main/webapp/resource/txt";

    public TermUpdater(String dirPath) {
        this.dirPath = dirPath;
    }

    public static void main(String[] args) {
        String dirPath = "src/main/webapp/resource/txt";
        TermUpdater updater = new TermUpdater(dirPath);
        updater.update();
    }

    public void update() {
        loadID();
        AllDocLister lister = new AllDocLister(this.dirPath);
        List<File> files = lister.work();
        Connection conn = Mysql.getConn("stdmem");
        int id = 0;
        for (File file : files) {
            System.out.println(file.getName());
            DocLoader loader = new DocLoader(file.getAbsolutePath());
            List<String> lines = loader.readLines();
            lines = loader.findMainBody(lines)[1];
            TermExtractor extractor = new TermExtractor(lines);
            List<Term> terms = extractor.extract();
            String fileName = file.getName().replace("GB", "").replace("DL", "").replace("T", "").replaceAll("\\s+^-", "").replace("Z", "").replace("DL_T", "").replace("GB_T", "").replace("HJ_T", "").replace("HJ", "").replace("_", "").replace(".txt", "").trim();
            if (!map.containsKey(fileName)) {
                System.err.println(file.getAbsolutePath());
                continue;
            }
            for (Term term : terms) {
                id++;
                try {
                    PreparedStatement pstmt = conn.prepareStatement("insert into std_term values (?,?,?,?,?,?)");
                    pstmt.setLong(1, id);
                    pstmt.setString(2, term.getTermCn());
                    pstmt.setString(3, term.getTermEn());
                    pstmt.setString(4, term.getDescription());
                    pstmt.setLong(5, map.get(fileName));
                    pstmt.setString(6, term.getContext());
                    pstmt.execute();
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void loadID() {
        try {
            Connection conn = Mysql.getConn("stdmem");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from std_std");
            while (rs.next()) {
                Long id = rs.getLong("id");
                String pdfPath = rs.getString("pdf_path");
                if (pdfPath != null) {
                    pdfPath = pdfPath.replace("GB", "").replace("DL", "").replace("T", "").replaceAll("\\s+^-", "").replace("Z", "").replace("DL_T", "").replace("GB_T", "").replace("HJ_T", "").replace("HJ", "").replace("_", "").replace(".pdf", "").trim();
                    map.put(pdfPath, id);
                    System.out.println(pdfPath + "\t" + id);
                }
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
